#Requires AutoHotkey v2.0
#Include "func.ahk"

; 以管理员身份运行
runAsAdmin()

; 暂不可用，要设置的 PATH 变量列表
writePath := [
	SCOOP_BIN
]

; 获取用户级 PATH 环境变量
Path := RegRead("HKEY_CURRENT_USER\Environment", "Path")

; 设置 scoop 包管理器的基本环境变量
if (pm_scoop = true) {
	if not (InStr(Path, SCOOP_BIN)) {
		RegWrite Path . SCOOP_BIN . ";", "REG_SZ", "HKEY_CURRENT_USER\Environment", "Path"
	}
	; 遍历用户环境变量下的项，看是否不包含 SCOOP 和 SCOOP_GLOBAL
	existScoop := true
	existScoopGlobal := true
	Loop Reg, "HKEY_CURRENT_USER\Environment", "K"
	{
		if (A_LoopRegName = "SCOOP") {
			existScoop := false
		}
		if (A_LoopRegName = "SCOOP_GLOBAL") {
			existScoopGlobal := false
		}
		if (!existScoop && !existScoopGlobal) {
			break
		}
	}
	; 如果存在，则覆写注册表项
	if (existScoop) {
		RegWrite SCOOP, "REG_SZ", "HKEY_CURRENT_USER\Environment", "SCOOP"
	}
	if (existScoopGlobal) {
		RegWrite SCOOP_GLOBAL, "REG_SZ", "HKEY_CURRENT_USER\Environment", "SCOOP_GLOBAL"
	}
	; 如果不存在，则创建注册表项后设置值
	if not (existScoop) {
		RegCreateKey "HKEY_CURRENT_USER\Environment\SCOOP"
		RegWrite SCOOP, "REG_SZ", "HKEY_CURRENT_USER\Environment", "SCOOP"
	}
	if not (existScoopGlobal) {
		RegCreateKey "HKEY_CURRENT_USER\Environment\SCOOP_GLOBAL"
		RegWrite SCOOP_GLOBAL, "REG_SZ", "HKEY_CURRENT_USER\Environment", "SCOOP_GLOBAL"
	}
}

